$(function () {
    var image = $('#image')
    image.cropper({
        // 比例
        aspectRation: 1,
        // 图片映射位置标签
        preview: '.prevent',
        // 是否放大缩小 默认true:放大缩小 false:不放大缩小
        zoomable: false,
    });

    $('#btnUpload').on('click', function () {
        $('#file').click()
    })
    $("#file").on('change', function (e) {
        let file = e.target.files[0];
        let imageUrl = URL.createObjectURL(file)
        image
            .cropper('destroy')
            .attr('src', imageUrl)
            .cropper({
                aspectRation: 1,
                preview: '.prevent',
            })
    })
    $('#btnConfirm').on('click', function () {
        var dataURL = image
            .cropper(
                'getCroppedCanvas', {
                width: 100,
                height: 100,
            })
            .toDataURL('image/png')
        $.ajax({
            method: 'post',
            url: '/my/update/avatar',
            data: {
                avatar: dataURL
            },
            success: function (res) {
                if (res.status !== 0) {
                    return layer.msg('更换头像失败')
                }
                layer.msg('更换头像成功');
                window.parent.inIt()
                window.location.href='/home/dashboard.html'
            }
        })
    })
})